package com.itany.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itany.book.service.IBookService;
import com.itany.pojo.BookWithBLOBs;
import com.itany.vo.ActionResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tyh on 2018/9/23.
 */
@RestController
@RequestMapping("/testbook")
public class TestBookController {

    @Reference
    private IBookService bookService;

    @RequestMapping("/addDoc")
    public ActionResult addDoc() {
        ActionResult ac = new ActionResult();
        bookService.addDoc();
        ac.setStatus(0);
        return ac;
    }


    @RequestMapping("/findDoc")
    public Map<String, Object> findDoc(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String keyWord) {
        return bookService.findDoc(page, rows, keyWord);
    }


    @RequestMapping("/addCart")
    public ActionResult addCart(BookWithBLOBs b, HttpSession session) {
        Map<Integer, BookWithBLOBs> cart = (Map<Integer, BookWithBLOBs>) session.getAttribute("Cart");
        if (cart == null) {
            cart = new HashMap<>();
            session.setAttribute("Cart", cart);
        }

        BookWithBLOBs book = cart.get(b.getId());
        if (book == null) {
            book = b;
            cart.put(book.getId(), book);
        }

        Integer number = book.getNumber();
        book.setNumber(number == null ? 1 : number + 1);
        ActionResult result = new ActionResult();

        return result;
    }

    @RequestMapping("/decCart")
    public ActionResult decCart(BookWithBLOBs b, HttpSession session) {
        Map<Integer, BookWithBLOBs> cart = (Map<Integer, BookWithBLOBs>) session.getAttribute("Cart");
        if (cart == null) {
            cart = new HashMap<>();
            session.setAttribute("Cart", cart);
        }

        BookWithBLOBs book = cart.get(b.getId());
        if (book == null) {
            book = b;
        }

        Integer number = book.getNumber();
        book.setNumber(number - 1);
        if (book.getNumber() <= 0) {
            cart.remove(b.getId());
        }
        ActionResult result = new ActionResult();

        return result;
    }

    @RequestMapping("/allInCarts")
    public ActionResult AllInCarts(HttpSession session) {
        Map<Integer, BookWithBLOBs> cart = (Map<Integer, BookWithBLOBs>) session.getAttribute("Cart");
        ActionResult result = new ActionResult();
        result.setData(new ArrayList<>(cart.values()));
        return result;
    }

    @RequestMapping("/removeInCart")
    public ActionResult removeInCart(Integer id, HttpSession session) {
        Map<Integer, BookWithBLOBs> cart = (Map<Integer, BookWithBLOBs>) session.getAttribute("Cart");
        ActionResult result = new ActionResult();
        cart.remove(id);
        return result;
    }


    @RequestMapping("allInOrder")
    public ActionResult allInOrder(@RequestParam("ids[]") Integer[] ids, HttpSession session) {
        Map<Integer, BookWithBLOBs> cart = (Map<Integer, BookWithBLOBs>) session.getAttribute("Cart");
        ActionResult result = new ActionResult();
        ArrayList<Object> list = new ArrayList<>();
        cart.values().forEach(book -> {
            for (Integer id : ids) {
                if (id == book.getId()) {
                    list.add(book);
                }
            }
        });

        result.setData(list);
        return result;
    }


}
